package curator;


import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

public class CuratorCreate {
    private String IP="192.168.0.201:2181,192.168.0.202:2181,192.168.0.203:2181";
    CuratorFramework client;

    @Before
    public void createClient(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 3);
        client = CuratorFrameworkFactory.builder()
                .connectString(IP)
                .sessionTimeoutMs(5000)
                .retryPolicy(retryPolicy)
                .namespace("create")
                .build();
        client.start();
    }

    @Test
    public void test01() throws Exception {
        // 新增节点
        client.create()
                // 节点的类型
                .withMode(CreateMode.PERSISTENT)
                // 节点的权限列表 world:anyone:cdrwa
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                // arg1:节点的路径
                // arg2:节点的数据
                .forPath("/node1","node1".getBytes());
        System.out.println("结束");

    }

    @Test
    public void test02() throws Exception {
        // 自定义权限列表

        // 权限列表
        ArrayList<ACL> list = new ArrayList<ACL>();
        Id id = new Id("ip", "192.168.0.202");
        list.add(new ACL(ZooDefs.Perms.ALL,id));
        client.create()

                .withMode(CreateMode.PERSISTENT)
                .withACL(list)
                .forPath("/node2","node2".getBytes());
        System.out.println("结束");

    }

    @Test
    public void test04() throws Exception {
        //递归创建节点树
        client.create()
                //递归节点的创建
                .creatingParentContainersIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath("/node3/node31","node31".getBytes());
        System.out.println("结束");

    }

    @Test
    public void test05() throws Exception {
        //异步创建节点
        client.create()
                .creatingParentContainersIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .inBackground(new BackgroundCallback() {
                    public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                        //附加的内容
                        System.out.println(event.getContext());
                        System.out.println(event.getPath());
                        System.out.println(event.getType());
                    }
                },"text").forPath("/node4","node4".getBytes());
        Thread.sleep(50000);
    }



    @After
    public void closeClient(){
       client.close();
    }
}
